using System;
using System.Data;
using System.Data.SqlClient;
using BeeldBank.Data.Sql;

namespace BeeldBank.Data
{
    /// <summary>
    /// Provides database independant methods for MediaObject.
    /// </summary>
    public class MediaObject : MediaObjectBase
    {
        public static DataTable GetBannerRotator(int TypeId)
        {
            SqlParameter[] sqlParamArray = new SqlParameter[1];

            sqlParamArray[0] = new SqlParameter("@TypeId", SqlDbType.Int);
            sqlParamArray[0].Value = TypeId;

            return OpenParamArray("BEELDBANK_CUSTOM_MEDIAOBJECT_BANNERROTATOR", sqlParamArray, null);
        }

        public static DataTable GetBannerRotatorThema(int CollectionId)
        {
            SqlParameter[] sqlParamArray = new SqlParameter[1];

            sqlParamArray[0] = new SqlParameter("@CollectionId", SqlDbType.Int);
            sqlParamArray[0].Value = CollectionId;

            return OpenParamArray("BEELDBANK_CUSTOM_MEDIAOBJECT_BANNERROTATOR_COLLECTION", sqlParamArray, null);
        }

        public static DataTable GetSuggestions(string SearchCriteria, string TypeList)
        {
            SqlParameter[] sqlParamArray = new SqlParameter[2];

            sqlParamArray[0] = new SqlParameter("@SearchCriteria", SqlDbType.VarChar);
            sqlParamArray[0].Value = SearchCriteria;
            sqlParamArray[0].Size = SearchCriteria.Length;

            sqlParamArray[1] = new SqlParameter("@TypeList", SqlDbType.VarChar);
            sqlParamArray[1].Value = TypeList;
            sqlParamArray[1].Size = TypeList.Length;

            return OpenParamArray("BEELDBANK_CUSTOM_WORD_SEARCH_COUNT_RESULT", sqlParamArray, null);
        }

        public static DataTable Search(string SearchCriteria, string TypeList, DateTime StartDate, DateTime EndDate, int PageIndex, int PageSize, int OrderBy, int CollectionId, out int TotalRecords)
        {
            SqlParameter[] sqlParamArray = new SqlParameter[9];

            sqlParamArray[0] = new SqlParameter("@SearchCriteria", SqlDbType.VarChar);
            sqlParamArray[0].Value = SearchCriteria;
            sqlParamArray[0].Size = SearchCriteria.Length;

            sqlParamArray[1] = new SqlParameter("@TypeList", SqlDbType.VarChar);
            sqlParamArray[1].Value = TypeList;
            sqlParamArray[1].Size = TypeList.Length;

            sqlParamArray[2] = new SqlParameter("@dtFrom", SqlDbType.DateTime);
            sqlParamArray[2].Value = StartDate;

            sqlParamArray[3] = new SqlParameter("@dtTo", SqlDbType.DateTime);
            sqlParamArray[3].Value = EndDate;

            if (PageIndex == -1)
            {
                PageIndex = 0;
                PageSize = int.MaxValue;
            }

            sqlParamArray[4] = new SqlParameter("@PageIndex", SqlDbType.Int);
            sqlParamArray[4].Value = PageIndex;

            sqlParamArray[5] = new SqlParameter("@PageSize", SqlDbType.Int);
            sqlParamArray[5].Value = PageSize;

            sqlParamArray[6] = new SqlParameter("@OrderBy", SqlDbType.Int);
            sqlParamArray[6].Value = OrderBy;

            sqlParamArray[7] = new SqlParameter("@CollectionId", SqlDbType.Int);
            sqlParamArray[7].Value = CollectionId;

            sqlParamArray[8] = new SqlParameter("@RowsReturned", SqlDbType.Int);
            sqlParamArray[8].Value = 0;
            sqlParamArray[8].Direction = ParameterDirection.InputOutput;

            DataTable dt = OpenParamArray("BEELDBANK_CUSTOM_WORD_SEARCH", sqlParamArray, null);
            TotalRecords = (int)sqlParamArray[8].Value;

            return dt;
        }

        public static DataTable SearchId(int Number, int PageIndex, int PageSize, out int TotalRecords)
        {
            SqlParameter[] sqlParamArray = new SqlParameter[4];

            sqlParamArray[0] = new SqlParameter("@Number", SqlDbType.Int);
            sqlParamArray[0].Value = Number;

            if (PageIndex == -1)
            {
                PageIndex = 0;
                PageSize = int.MaxValue;
            }

            sqlParamArray[1] = new SqlParameter("@PageIndex", SqlDbType.Int);
            sqlParamArray[1].Value = PageIndex;

            sqlParamArray[2] = new SqlParameter("@PageSize", SqlDbType.Int);
            sqlParamArray[2].Value = PageSize;

            sqlParamArray[3] = new SqlParameter("@RowsReturned", SqlDbType.Int);
            sqlParamArray[3].Value = 0;
            sqlParamArray[3].Direction = ParameterDirection.InputOutput;

            DataTable dt = OpenParamArray("BEELDBANK_CUSTOM_ID_SEARCH", sqlParamArray, null);
            TotalRecords = (int)sqlParamArray[3].Value;

            return dt;
        }
    }
}
